﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using XStudio.XCRF.Data.Business;
using XStudio.XCRF.Data.Entities.LabCheck;
using XStudio.XCRF.Data.Business.Permissions;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Web.Common.Sessions;
using XStudio.XCRF.Web.Models.LabCheck;
using Telerik.Web.Mvc;

namespace XStudio.XCRF.Web.Controllers
{
    /// <summary>
    /// 实验室检查控制器
    /// </summary>
    [DefaultPermission(PermissionType.Allow)]
    public class LabCheckController : BaseController
    {
        public ActionResult LabCheckAdd(Guid crfID)
        {
            CRF crf = DataContext.CRFs.Include(c => c.Patient).Find(crfID);
            LabCheck labCheck = new LabCheck
            {
                CRFID = crfID,
                CRF = crf
            };
            return View(labCheck);
            //throw new NotImplementedException();
        }

        [HttpPost]
        public ActionResult LabCheckAdd(LabCheck labCheck)
        {
            try
            {
                DataContext.LabChecks.Add(labCheck);
                DataContext.SaveChanges();
                return Json(new { ID = labCheck.ID });
            }
            catch
            {
                return Json(null);
            }
        }

        public ActionResult LabCheckDetail(Guid labCheckID)
        {
            LabCheck labCheck = DataContext.LabChecks.Include(l => l.CRF).Where(l => l.ID == labCheckID).First();
            Guid hospitalID = (Guid)LoginSession.HospitalID;
            LabCheckStd labCheckStd = null;
            if (!labCheck.IsDeleted)
            {
                List<LabCheckStd> labCheckStds = DataContext.LabCheckStds.Where(lcs => lcs.HospitalID == hospitalID && lcs.Gender == labCheck.CRF.Patient.Gender).ToList();
                if (labCheckStds != null && labCheckStds.Count > 0)
                    labCheckStd = labCheckStds[0];
            }
            LabCheckWithStd labCheckWithStd = new LabCheckWithStd() { labCheck = labCheck, labCheckStd = labCheckStd };
            return View(labCheckWithStd);
        }

        public ActionResult LabCheckEdit(Guid labCheckID)
        {
            LabCheck labCheck = DataContext.LabChecks.Find(labCheckID);
            return View(labCheck);
        }

        [HttpPost]
        public ActionResult LabCheckEdit(LabCheck labCheck)
        {
            try
            {
                LabCheck l = DataContext.LabChecks.Find(labCheck.ID);
                //LabCheck l = DataContext.LabChecks.Find<LabCheck>(labCheck.ID);
                l.ESR = labCheck.ESR;

                l.RBC = labCheck.RBC;
                l.PLT = labCheck.PLT;
                l.Hb = labCheck.Hb;
                l.WBC = labCheck.WBC;
                l.NEU = labCheck.NEU;
                l.LYM = l.LYM;
                l.MNC = labCheck.MNC;

                l.CEA = labCheck.CEA;
                l.CA50 = labCheck.CA50;
                l.CA199 = labCheck.CA199;
                l.CA242 = labCheck.CA242;
                l.CA724 = labCheck.CA724;
                
               
                l.PRO = labCheck.PRO;
                l.GLU = labCheck.GLU;
                l.BLD = labCheck.BLD;
                l.LEU = labCheck.LEU;
                //l.LEU1 = labCheck.LEU1;
                //l.LEU2 = labCheck.LEU2;
                //l.BLD1 = labCheck.BLD1;
                //l.BLD2 = labCheck.BLD2;

                l.BXB = labCheck.BXB;
                l.FB = labCheck.FB;
                

                l.AST = labCheck.AST;
                l.ALT = labCheck.ALT;
                l.BUN = labCheck.BUN;
                l.SCr = labCheck.SCr;
                l.ZDHS = labCheck.ZDHS;
                l.JHDHS = labCheck.JHDHS;
                l.BDB = labCheck.BDB;
                l.JXLSM = labCheck.JXLSM;
                l.GAXZTM = labCheck.GAXZTM;
                l.ZDGC = labCheck.ZDGC;
                l.NS = labCheck.NS;

                l.NK = labCheck.NK;
                l.CD3 = labCheck.CD3;
                l.CD4 = labCheck.CD4;
                l.CD8 = labCheck.CD8;
                l.CD4_CD8 = labCheck.CD4_CD8;
                //l.ECGType = labCheck.ECGType;
                l.Enteroscope = labCheck.Enteroscope;
                l.EnteroscopeTime=labCheck.EnteroscopeTime;
                l.ECG = labCheck.ECG;
                l.ECGTime = labCheck.ECGTime;
                l.BUltra = labCheck.BUltra;
                l.BUltraTime = labCheck.BUltraTime;
                l.ChestCT = labCheck.ChestCT;
                l.ChestCTTime = labCheck.ChestCTTime;
                l.AbdomenCT = labCheck.AbdomenCT;
                l.AbdomenCTTime = labCheck.AbdomenCTTime;
                l.PelvicCT = labCheck.PelvicCT;
                l.PelvicCTTime = labCheck.PelvicCTTime;
                l.PelvicMRI = labCheck.PelvicMRI;
                l.PelvicMRITime = labCheck.PelvicMRITime;
                l.PetCT = labCheck.PetCT;
                l.PetCTTime = labCheck.PetCTTime;
                //l.OtherTime = labCheck.OtherTime;
                DataContext.SaveChanges();
                return Json(new { ID = l.ID });
            }
            catch
            {
                return Json(null);
            }
        }

        #region 检查历史记录
        public ActionResult LabCheckHisGridList(Guid labCheckID)
        {
            return View(labCheckID);
        }

        [GridAction]
        public ActionResult _LabCheckHisGridList(Guid labCheckID)
        {
            List<LabCheck> labChecks = DataContext.LabChecks
                .Include(l => l.Creater)
                .Include(l => l.LastModifier).ToList().Where(l => l.MasterID == labCheckID).Select(labcheck =>
                new LabCheck
                {
                    ID = labcheck.ID,
                    Time = labcheck.Time,
                    CRF = labcheck.CRF,
                    CRFID = labcheck.CRFID,
                    LastModifyTime = labcheck.LastModifyTime,
                    LastModifier = new Member { RealName = labcheck.LastModifier.RealName },
                    CreateTime = labcheck.CreateTime,
                    Creater = new Member { RealName = labcheck.Creater.RealName },
                    MasterID = labcheck.MasterID,
                    //Master = labcheck.Master
                }).ToList();
            return View(new GridModel(labChecks));
        }
        #endregion
        /*[HttpPost]
        public ActionResult LabCheckDelete(LabCheck labCheck)
        {
            LabCheck labCheck = DataContext.LabChecks.Include(l => l.CRF).Find();
            DataContext.CureInfoes.Remove(cureInfo);
            DataContext.SaveChanges();
            return View(cureInfo);
            //throw new NotImplementedException();
        }*/

        //TODO: 实验室检查控制器待完成
    }
}
